<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="城市编号" prop="cityId">
          <el-cascader
            :options="provinces"
            placeholder="请选择城市"
            v-model="queryParams.cityIds"
            clearable>
          </el-cascader>
      </el-form-item>
      <el-form-item label="经济带" prop="dictionaryTypeCode">
        <el-select v-model="queryParams.dictionaryId" placeholder="请选择经济带" clearable>
          <el-option
            v-for="dict in dictDataList"
            :key="dict.dictCode"
            :label="dict.dictLabel"
            :value="dict.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dictionary:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dictionary:edit']"
        >修改</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dictionary:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dictionary:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dictionaryList"  @selection-change="handleSelectionChange" height="650" fixed>
      <el-table-column type="index" align="center" width="55"/>
      <el-table-column label="经济带" align="center" prop="dictionary" width="200"/>
      <el-table-column label="字典名称" align="center" prop="dictName" width="200"/>
      <el-table-column label="字典类型标识" align="center" prop="dictionaryTypeCode" width="200"/>
      <el-table-column label="城市名称" align="center" prop="city"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dictionary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dictionary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改字典关联城市对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="margin-left: 10%; margin-right: 10%">
         <el-form-item label="经济带" prop="dictionaryId">
          <el-select v-model="form.dictionaryId" placeholder="请选择经济带">
            <el-option
              v-for="dict in dictDataList"
              :key="dict.dictCode"
              :label="dict.dictLabel"
              :value="dict.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityIds"><!--todo-->
          <el-cascader
            :options="provinces"
            placeholder="请选择城市"
            v-model="form.cityIds"
            :props = "props"
            >
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDictionary,
  getDictionary,
  getDictCityInfo,
  delDictionary,
  addDictionary,
  updateDictionary,
  listDictCity,
  getCityAll
} from "@/api/system/dictionary";
import {listCity} from "@/api/business/businessIntro";
import {optionselect} from "@/api/system/dict/type";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "Dictionary",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典关联城市表格数据
      dictionaryList: [],
      // 弹出层标题
      title: "",
      //省份
      provinces:[],
      //字典类型
      dictTypeList:[],
      //字典数据
      dictDataList:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cityId: null,
        cityIds: [],
        relevanceType: '1',
        dictionaryId: null,
        dictionaryIds: [],
        dictionaryTypeId: null,
        dictionaryTypeCode: null
      },
      queryParam:{
        id:null,
        dictionaryId: null,
        relevanceType: '1',
        dictionaryTypeId: 102,
        dictionaryTypeCode: 'economic_belt'
      },
      // 表单参数
      form: {},
      props: { multiple: true },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getProvinceList();
    this.getDicts();
  },
  methods: {
    /** 查询字典关联城市列表 */
    getList() {
      this.loading = true;
      listDictCity(this.queryParams).then(response => {
        this.dictionaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityId: null,
        cityIds: [],
        relevanceType: '0',
        dictionaryId: null,
        dictionaryTypeId: 102,
        dictionaryTypeIds: [],
        dictionaryTypeCode: 'economic_belt'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams)
      if (this.queryParams.cityIds != null) {
        this.queryParams.cityId = '['+this.queryParams.cityIds.join(", ")+']'
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForms");
      this.queryParams.cityIds = null
      this.queryParams.cityId = null
      this.queryParams.dictionaryId = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.relevanceType = this.queryParams.relevanceType;
      console.log(this.form.relevanceType)
      this.open = true;
      this.title = "添加关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryParams.dictionaryId = row.dictionaryId
      // this.queryParam.relevanceType = this.queryParams.relevanceType
      getDictCityInfo(this.queryParams).then(response => {
        this.form = response.data;
        this.form.dictionaryTypeId = this.queryParam.dictionaryTypeId
        this.form.dictionaryTypeCode = this.queryParam.dictionaryTypeCode
        // if (this.queryParams.relevanceType==='0'){
        //   this.getDictDataList(this.form.dictionaryTypeId)
        // }
        this.title = "修改关联";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        // this.form.cityId ="[" + this.form.cityIds.join("],[") +"]"
        if (valid) {
          if (this.title === "修改关联") {
            updateDictionary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDictionary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.dictionaryId || this.ids;
      this.$modal.confirm('是否确认删除经济带为"' + row.dictionary + '"的数据项？').then(function() {
        return delDictionary(row.dictionaryId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dictionary/export', {
        ...this.queryParams
      }, `dictionary_${new Date().getTime()}.xlsx`)
    },
    /** 查询省份列表*/
    getProvinceList(){
      // const parentId = 0;
      getCityAll().then(res =>{
        console.log("data",res.data[0])
        console.log("provinces",this.provinces)
        this.provinces = res.data[0];
      })
    },
    getCityList(){
      listCity(id).then(res=>{

      })
    },
    getDicts(){
      getDicts('economic_belt').then(res => {
        this.dictDataList = res.data;
        console.log(res.data)
      });
    },
    // getDictTypeList(){
    //   optionselect().then(res =>{
    //     this.dictTypeList = res.data;
    //     // console.log(res.data);
    //   })
    // },
    getDictDataList(dictTypeId) {
      this.dictTypeList.forEach(i => {
        if (dictTypeId === i.dictId) {
          console.log(i)
          this.form.dictionaryTypeCode = i.dictType
          getDicts(i.dictType).then(res => {
            this.dictDataList = res.data;
            console.log(res.data)
          });
        }
      })
    }
  }
};
</script>
